package panel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;


public class MainFrame extends JFrame {
	private static int HEIGHT;
	private static int WIDTH;
	private Set<Integer> randomSet;
	private Random random;
	private String currentKey;
	private String[] imgNames;
	private String[] wavNames;
	private Set<JButton> currentSet;
	
	public MainFrame(){
		setUp();
	}
	
	private void setUp(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
		initArrays();
		random = new Random();
		randomSet = new HashSet<Integer>();
		currentSet = new HashSet<JButton>();

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		WIDTH = (int) screenSize.getWidth() / 2;
		HEIGHT = (int) screenSize.getHeight() / 2;
		
		setSize(HEIGHT, WIDTH);

		JLayeredPane lp = getLayeredPane();
		initPane(lp);
		setSelections(3);
	}
	private void setSelections(int numCards) {
		JLayeredPane lp = getLayeredPane();
		currentSet = getCards(numCards);
		for(JButton j: currentSet){
			lp.add(j, new Integer(2));
		}	
		repaint();
	}
	
	private void initArrays() {
		imgNames = new String[26];
		wavNames = new String[26];

		for(int i = 'a'; i <= 'z'; i ++){
			imgNames[i - 'a'] = "src/images/" + String.valueOf(Character.valueOf((char) i)) + ".png"; 
			wavNames[i - 'a'] = "src/sounds/" + String.valueOf(Character.valueOf((char) i)) + ".wav"; 
		}
	}

	
	private Set<JButton> getCards(final int numPanels) {
		Set<JButton> set = new HashSet<JButton>();
		int x = WIDTH / 35;
		int y = HEIGHT;
		while (randomSet.size() < numPanels) {
			int rand = random.nextInt(26);
			int prev = randomSet.size();
			randomSet.add(rand);
			
			if (randomSet.size() != prev) {
				JButton tgglbtn = createToggleWithImage(imgNames[rand], 
						 x, y, WIDTH / 6, HEIGHT / 3);
				 x += WIDTH / 6 + (WIDTH / 35);
				tgglbtn.setName(String.valueOf(rand));
				tgglbtn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						JButton b = (JButton) e.getSource();
						if(currentKey.equals(b.getName())){
							ToolBag.playSound("src/sounds/yay.wav");
							repaint();
						} else {
							ToolBag.playSound("src/sounds/boo.wav");
						}
					}

				});
				set.add(tgglbtn);
				
			}
		}
		Object[] rands = randomSet.toArray();
		currentKey = String.valueOf(rands[random.nextInt(rands.length - 1)]);
		ToolBag.playSound(wavNames[Integer.valueOf(currentKey)]);
		
		return set;
		
	}
	
	private void initPane(final JLayeredPane lp) {
		ImagePanel northPanel = new ImagePanel("src/images/top.png", HEIGHT , WIDTH / 2);
		northPanel.setLayout(new BorderLayout());
        northPanel.setBounds(0, 0, HEIGHT, WIDTH / 2);


        JButton menu = createToggleWithImage("src/images/menu.png", 
        		0, 0, getWidth() / 4, getHeight() / 15);
        JButton exit = createToggleWithImage("src/images/exit.png", 
        		getWidth() - getWidth() / 4, 0, getWidth() / 4, getHeight() / 15);
        JButton replay = createToggleWithImage("src/images/replay.png", 
        		getWidth() /2 - getWidth() / 4, (int) (getHeight() / 2.5), getWidth() / 2, getHeight() / 15);
        
        replay.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ToolBag.playSound(wavNames[Integer.valueOf(currentKey)]);
			}
		});
        
		ImagePanel southPanel = new ImagePanel("src/images/bottom.png", HEIGHT ,  WIDTH / 2);
		southPanel.setLayout(new FlowLayout());
        southPanel.setBounds(0, WIDTH / 2, HEIGHT, WIDTH);

		
        lp.add(northPanel, new Integer(1));
        lp.add(southPanel, new Integer(1));
        lp.add(menu, new Integer(2));
        lp.add(exit, new Integer(2));
        lp.add(replay, new Integer(2));		
	}

	public JButton createToggleWithImage(String imgSrc, int x, int y,  int width, int height){
		BufferedImage img = ToolBag.createResizedCopy(new ImageIcon(imgSrc).getImage(), width, height, false);
		JButton button = new JButton(new ImageIcon(img));
		button.setBorder(BorderFactory.createEmptyBorder());
        button.setBounds(x, y, width, height);
        return button;
	}
	
    public static void main(String[] args) {
        MainFrame sl = new MainFrame();
        sl.setVisible(true);
    }
}
